<template>
  <div class="register-container">
    <div class="register-form-wrapper">
      <el-form
        class="register-form"
        ref="registerFormRef"
        :model="registerForm"
        :rules="registerRules"
        autocomplete="on"
        label-position="left">
        <div class="title-container">
          <h3 class="title">欢迎注册{{ title }}</h3>
        </div>

        <el-form-item prop="username">
          <span class="svg-container">
            <i class="ri-user-3-line"></i>
          </span>
          <el-input
            ref="usernameRef"
            v-model="registerForm.username"
            placeholder="Username"
            name="username"
            type="text"
            tabindex="1"
            autocomplete="on" />
        </el-form-item>

        <el-form-item prop="email">
          <span class="svg-container">
            <i class="ri-mail-line"></i>
          </span>
          <el-input
            ref="emailRef"
            v-model="registerForm.email"
            placeholder="Email"
            name="email"
            type="email"
            tabindex="2"
            autocomplete="on" />
        </el-form-item>

        <el-tooltip v-model="capslockTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="password">
            <span class="svg-container">
              <i class="ri-lock-line"></i>
            </span>
            <el-input
              :key="passwordType"
              ref="passwordRef"
              v-model="registerForm.password"
              :type="passwordType"
              placeholder="Password"
              name="password"
              tabindex="3"
              autocomplete="on"
              @keyup="checkCapsLock"
              @blur="capslockTooltip = false" />
            <span class="show-pwd" @click="changePasswordType">
              <i :class="passwordType === 'password' ? 'ri-eye-close-line' : 'ri-eye-line'"></i>
            </span>
          </el-form-item>
        </el-tooltip>

        <el-tooltip v-model="capslockTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="confirm">
            <span class="svg-container">
              <i class="ri-lock-line"></i>
            </span>
            <el-input
              :key="passwordType"
              ref="confirmRef"
              v-model="registerForm.confirm"
              :type="passwordType"
              placeholder="Confirm Password"
              name="password"
              tabindex="4"
              autocomplete="on"
              @keyup="checkCapsLock"
              @blur="capslockTooltip = false"
              @keyup.enter="handleRegister" />
            <span class="show-pwd" @click="changePasswordType">
              <i :class="passwordType === 'password' ? 'ri-eye-close-line' : 'ri-eye-line'"></i>
            </span>
          </el-form-item>
        </el-tooltip>

        <el-button :loading="loading" type="primary" style="width: 100%" @click.prevent="handleRegister">
          Register
        </el-button>

        <router-link to="/login">
          <span class="back">返回</span>
        </router-link>

        <!--      <el-button type="success" style="width:100%" @click="showDialog = true">-->
        <!--        Or connect with-->
        <!--      </el-button>-->
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
  import { computed, defineComponent, onMounted, ref } from 'vue';

  import { useStore } from 'vuex';
  import { useCapsLock } from '@/views/login/hooks/useCapsLock';
  import { usePasswordType } from '@/views/login/hooks/usePasswordType';
  import { useLoading } from '@/hooks/useLoading';
  import { useRouter } from 'vue-router';
  import { ElMessage } from 'element-plus';
  import { ELForm, ELInput } from '@/utils/types';
  import { useRegisterForm } from '@/views/register/hooks/useRegisterForm';
  import { accountService } from '@/services/security/LoginAccount';

  export default defineComponent({
    name: 'Register',
    setup: function () {
      const router = useRouter();
      const store = useStore();
      const { registerForm, registerRules } = useRegisterForm();
      const title = computed(() => store.getters['settings/title']);
      const { capslockTooltip, checkCapsLock } = useCapsLock();
      const { passwordType, changePasswordType } = usePasswordType();
      const { loading, setLoading } = useLoading();

      const registerFormRef = ref(null as ELForm | null);
      const usernameRef = ref(null as ELInput | null);
      const emailRef = ref(null as ELInput | null);
      const passwordRef = ref(null as ELInput | null);
      const confirmRef = ref(null as ELInput | null);

      const isRememberMe = ref(false);

      onMounted(() => {
        if (registerForm.username === '') {
          usernameRef.value?.focus();
        } else if (registerForm.email === '') {
          emailRef.value?.focus();
        } else if (registerForm.password === '') {
          passwordRef.value?.focus();
        } else {
          confirmRef.value?.focus();
        }
      });

      const handleRegister = async () => {
        registerFormRef.value?.validate(async valid => {
          if (valid) {
            setLoading();
            try {
              await accountService.register(registerForm);
              ElMessage.success('注册成功!');
              await router.push('/login');
            } catch (ignored) {
              ElMessage.error('注册失败!');
            } finally {
              setLoading();
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      };

      return {
        title,
        registerForm,
        registerRules,
        isRememberMe,
        registerFormRef,
        usernameRef,
        passwordRef,
        capslockTooltip,
        checkCapsLock,
        passwordType,
        changePasswordType,
        loading,
        setLoading,
        handleRegister,
        showDialog: false,
      };
    },
  });
</script>

<style lang="scss">
  $bg: #f6f4fc;

  @import '~@/assets/styles/index.scss';

  /* reset element-ui css */
  .register-container {
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0;
        -webkit-appearance: none;
        border-radius: 0;
        padding: 12px 5px 12px 15px;
        color: $black;
        height: 47px;

        &:-webkit-autofill {
          box-shadow: 0 0 0 1000px $bg inset !important;
          -webkit-text-fill-color: $black !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
  }
</style>

<style lang="scss" scoped>
  $bg: #f6f4fc;
  $dark_gray: #889aa4;

  @import '~@/assets/styles/index.scss';
  $img: '~@/assets/image/login_background.jpg';

  .register-container {
    //background: url($img) center center fixed no-repeat;
    background-color: $white;
    position: fixed;
    height: 100%;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    .register-form-wrapper {
      color: transparent;
      flex-basis: 50%;
      display: flex;
      justify-content: center;
      align-items: center;

      .register-form {
        flex-basis: 65%;
        min-width: 300px;
        max-width: 500px;

        .el-form-item {
          background-color: #f6f4fc;
        }

        .remember-me {
          float: left;
          margin-bottom: 10px;
        }

        .back {
          display: block;
          font-size: 14px;
          color: $text;
          margin-top: 10px;

          &:hover {
            color: $blue;
            cursor: pointer;
          }
        }
      }

      .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        span {
          &:first-of-type {
            margin-right: 16px;
          }
        }
      }

      .svg-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
      }

      .title-container {
        position: relative;

        .title {
          font-size: 26px;
          color: $black;
          margin: 0 auto 40px auto;
          text-align: center;
          font-weight: bold;
        }
      }

      .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
      }
    }
  }
</style>
